package cn.baimo.tools;

import com.cloopen.rest.sdk.BodyType;
import com.cloopen.rest.sdk.CCPRestSmsSDK;

import java.util.HashMap;
import java.util.Random;
import java.util.Set;

/**
 * ClassName: SMSUtil
 * Description:短信接口
 * date: 2020/8/23 0023 17:59:45
 *
 * @author BaiMo
 * @since JDK 1.8
 */
public class SMSUtil {
    /**
     * Description: 发送短信
     *
     * @author: BaiMo
     * @date: 2020/6/22 0022 14:33:18
     * @param: int
     * @return:
     */
    public static int senMsg(String phone, String checkCode) {
        int sendResult = 0;
        //生产环境请求地址：app.cloopen.com
        String serverIp = "app.cloopen.com";
        //请求端口
        String serverPort = "8883";
        //主账号,登陆云通讯网站后,可在控制台首页看到开发者主账号ACCOUNT SID和主账号令牌AUTH TOKEN
        String accountSId = "8aaf070872c6b59d0172c6de168d000f";
        String accountToken = "4eb5245fc8ee47e697eb2440b93322f3";
        //请使用管理控制台中已创建应用的APPID
        String appId = "8aaf070872c6b59d0172c6de17c10015";
        CCPRestSmsSDK sdk = new CCPRestSmsSDK();
        sdk.init(serverIp, serverPort);
        sdk.setAccount(accountSId, accountToken);
        sdk.setAppId(appId);
        sdk.setBodyType(BodyType.Type_JSON);
        String to = phone;
        String templateId = "607589";
        String[] datas = {checkCode, "60s"};
        HashMap<String, Object> result = sdk.sendTemplateSMS(to, templateId, datas);
        if ("000000".equals(result.get("statusCode"))) {
            //正常返回输出data包体信息（map）
            HashMap<String, Object> data = (HashMap<String, Object>) result.get("data");
            Set<String> keySet = data.keySet();
            for (String key : keySet) {
                Object object = data.get(key);
                System.out.println(key + " = " + object);
            }
            sendResult = 1;
        } else {
            //异常返回输出错误码和错误信息
            System.out.println("错误码=" + result.get("statusCode") + " 错误信息= " + result.get("statusMsg"));
        }
        return sendResult;
    }
}
